package com.sdgakj.service.person;

import com.sdgakj.entity.Person;
import com.sdgakj.entity.User;
import com.sdgakj.repository.PersonDao;

import java.util.List;
import java.util.Map;

import com.sdgakj.utils.SchoolSearchFilter;
import com.sdgakj.utils.SchoolSpecifications;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;
import org.springside.modules.persistence.SearchFilter.Operator;

@Component
@Transactional
public class PersonService {
	private static Logger logger = LoggerFactory.getLogger(PersonService.class);
	@Autowired
	private PersonDao personDao;

	public PersonService() {
	}

	public List<Person> findAll(){
		return (List<Person>) personDao.findAll();
	}
	public List<Person> getPersonsByFId(Long familyId) {
		return this.personDao.getPersonsByFamilyId(familyId);
	}

	public List<Person> getPersonsByFId(Long fid, Integer page, Integer pageSize) {
		return this.personDao.getPersonsById(fid, page, pageSize);
	}

	private PageRequest buildPageRequest(int pageNumber, int pagzSize, String sortType) {
		Sort sort = null;
		if("auto".equals(sortType)) {
			sort = new Sort(Direction.DESC, new String[]{"id"});
		}
		return new PageRequest(pageNumber - 1, pagzSize, sort);
	}

	private Specification<User> buildSpecification(String userType, Map<String, Object> searchParams) {
		Map filters = SearchFilter.parse(searchParams);
		filters.put("type", new SearchFilter("type", Operator.EQ, userType));
		Specification spec = DynamicSpecifications.bySearchFilter(filters.values(), User.class);
		return spec;
	}

	public Person getPersonById(Long id) {
		return (Person) this.personDao.findOne(id);
	}

	public Page<Person> getPersonByPage(Map<String, Object> searchParams, int pageNumber, int pageSize, String sortName, String sortType) {
		PageRequest pageRequest = new PageRequest(pageNumber - 1, pageSize, Sort.Direction.DESC, "createtime");
		Map<String, SchoolSearchFilter> filters = SchoolSearchFilter.parse(searchParams);
		Specification<Person> spec = SchoolSpecifications.bySearchFilter(filters.values(), Person.class);
		return personDao.findAll(spec, pageRequest);

	}

	/**
	 * 新增人
	 * */
	public Long savePerson(Person person) {
		Person save = (Person)this.personDao.save(person);
		return save.getId();
	}
	/**
	 * 通过id删除
	 *
	 * */
	public void deletePersonById(Long personId) {
		this.personDao.delete(personId);
	}
	public Person  updatePersonById(Person person){
		return this.personDao.save(person);
	}

	//通过f字段 id条件查询 查找孩子
	public  List<Person> getPersonByFatherId(Long id){return this.personDao.getChildrensByFather(id);}
	//m字段
	public List<Person> getPersonByMatherId(Long id){return  personDao.getChildrensByMathar(id);}
	public Person getPersonByFamilyId(Long familyId,Long id){return  this.personDao.getPersonByFamilyId(familyId,id);}


	public Person getPersonByUserId(Long userId){return personDao.getPersonByUserId(userId);}

	public Long getPersonCount() {
		return personDao.getPersonCount();
	}


	public Long  personsm(){return personDao.personsm();}

	public Long personsf(){return personDao.personsf();}

	public Long personalive(){return personDao.personalive();}

	public Long personnalive(){return personDao.personnalive();}


}
